package zhaolu.com.example.Delicacy.Fragment;

import static android.os.Looper.getMainLooper;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import zhaolu.com.example.Delicacy.Activity.ChangePassword;
import zhaolu.com.example.Delicacy.Activity.MainActivity;
import zhaolu.com.example.Delicacy.Activity.OrderHistory;
import zhaolu.com.example.Delicacy.Entity.Customer;
import zhaolu.com.example.Delicacy.MySQL.CustomerDAO;
import zhaolu.com.example.Delicacy.R;
import zhaolu.com.example.Delicacy.Utils.CommonUtils;

public class MyFragment extends Fragment {
    private TextView person_name;
    private TextView password_change;
    private TextView tv_points;
//    private TextView tv_test;
    private Button bt_quit;
    private Button bt_logout;
    private Handler mainHandler;
    private TextView order_history;

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, container, false);

        SharedPreferences sp = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        String phone = sp.getString("username", null);
        String password = sp.getString("password", null);

        person_name = view.findViewById(R.id.person_name);
        person_name.setText("你好！" + phone);

        password_change = view.findViewById(R.id.password_change);
        password_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ComponentName componentname = new ComponentName(getActivity(), ChangePassword.class);
                Intent intent = new Intent();
                intent.setComponent(componentname);
                startActivity(intent);
            }
        });

        mainHandler = new Handler(getMainLooper());

        final int[] points = {0};
        tv_points = view.findViewById(R.id.points);
        tv_points.setOnClickListener(new View.OnClickListener() {
            CustomerDAO customerDAO = new CustomerDAO();
            @Override
            public void onClick(View v) {
                Customer item = new Customer();
                item.setId(phone);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        points[0] = customerDAO.query_points(item);
                        mainHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                CommonUtils.showShortMsg(getContext(), "您的积分为：" + points[0]);
                            }
                        });
                    }
                }).start();
            }
        });

//        tv_test = view.findViewById(R.id.test_points);
//        tv_test.setOnClickListener(new View.OnClickListener() {
//            CustomerDAO customerDAO = new CustomerDAO();
//            @Override
//            public void onClick(View view) {
//                Customer item = new Customer();
//                item.setId(phone);
//                int point = points[0] + 10;
//                item.setPoints(point);
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        customerDAO.test(item);
//                        mainHandler.post(new Runnable() {
//                            @Override
//                            public void run() {
//                                CommonUtils.showShortMsg(getContext(), "修改完成！" + point);
//                            }
//                        });
//                    }
//                }).start();
//            }
//        });

        bt_logout = view.findViewById(R.id.destroy);
        bt_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomerDAO customerDAO = new CustomerDAO();
                Customer item = new Customer();
                item.setId(phone);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        customerDAO.delete_user(item);
                        ComponentName componentname = new ComponentName(getActivity(), MainActivity.class);
                        Intent intent = new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.setComponent(componentname);
                        startActivity(intent);
                    }
                }).start();
            }
        });

        bt_quit = view.findViewById(R.id.quit);
        bt_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtils.showShortMsg(getActivity(), "退出成功！");
                ComponentName componentname = new ComponentName(getActivity(), MainActivity.class);
                Intent intent = new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setComponent(componentname);
                startActivity(intent);
            }
        });

        order_history = view.findViewById(R.id.order_history);
        order_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), OrderHistory.class);
                startActivity(intent);
            }
        });

        return view;
    }
}
